package com.bonc.ioc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bonc.ioc.common.util.AppReply;
import com.bonc.ioc.model.DataAssetInfo;
import com.bonc.ioc.model.HotwordInfo;
import com.bonc.ioc.service.impl.DataAssetAssociationServiceImpl;
import com.bonc.ioc.service.impl.DataAssetServiceImpl;
import com.bonc.ioc.service.impl.FlowRecordServiceImpl;
import com.bonc.ioc.service.impl.HotwordServiceImpl;
import com.bonc.ioc.vo.DataAssetVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 数据资产相关接口
 *
 * @author ZhangXu
 * @date 2021/3/23 15:05
 */
@RestController
@RequestMapping("/dataAsset")
public class DataAssetController {
    private static final Logger log = LoggerFactory.getLogger(DataAssetController.class);

    @Autowired
    private DataAssetServiceImpl dataAssetService;

    @Autowired
    private DataAssetAssociationServiceImpl dataAssetAssociationService;

    @Autowired
    private HotwordServiceImpl hotwordService;

    @Autowired
    private FlowRecordServiceImpl flowRecordService;

    /**
     * 获取数据资产分页列表接口
     *
     * @param param
     * @return 数据资产分页列表
     */
    @PostMapping("/queryDataAsset")
    public AppReply queryDataAsset(@RequestBody DataAssetVo param) {
        if (param.getDataOrigin().isEmpty()){
            param.setDataOrigin(null);
        }
        log.info(" ={}", JSON.toJSONString(param));
        try {
            if (param.getText() != null) {
                hotwordService.saveHotword(param.getText());
            }
        } catch (IOException e) {
            log.info("数据资产 保存查询热点词 失败...");
            e.printStackTrace();
        }
        HashMap<String, Object> result = dataAssetService.queryDataAsset(param);
        flowRecordService.updateTotalFlowRecord();
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", result);
    }

    /**
     * 获取数据资产详情
     *
     * @param id 数据资产id
     * @return
     */
    @GetMapping("/queryDataDetail")
    public AppReply queryDataDetail(Long id) {
        log.info("数据资产 获取资产详情 接收参数={}", id);
        DataAssetInfo result = dataAssetService.queryDataDetail(id);
        String msg = null;
        if (result == null) {
            msg = "未找到查询数据";
        } else {
            msg = "查询成功";
        }
        dataAssetAssociationService.updateAssociation(id);
        log.info("数据资产 获取资产详情 结果={}", JSON.toJSONString(result));
        flowRecordService.updateTotalFlowRecord();
        return new AppReply(AppReply.SUCCESS_CODE, msg, result);
    }

    /**
     * 获取数据资产检索热门词汇
     *
     * @return 热门词
     */
    @GetMapping("/queryHotWord")
    public AppReply queryHotWord() {
        List<String> result = hotwordService.queryHotWord();
        log.info("数据资产 获取热门词汇 结果={}", JSON.toJSONString(result));
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", result);
    }

    /**
     * 获取数据资产关联关系
     *
     * @param id 数据资产id
     * @return 数据资产关联关系
     */
    @GetMapping("/queryAssociation")
    public AppReply queryAssociation(Long id) {
        log.info("数据资产 获取关联关系 接收参数={}", id);
        List<DataAssetInfo> result = dataAssetAssociationService.queryAssociation(id);
        log.info("数据资产 获取关联关系 结果={}", JSON.toJSONString(result));
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", result);
    }

    /**
     * 获取 热门词关联词
     *
     * @return 热门词关联词
     */
    @GetMapping("/queryHotWordAssociation")
    public AppReply queryHotWordAssociation(String word) {
        if (StringUtils.isNotBlank(word)) {
            log.info("热点词 获取关联关系 接收参数={}", word);
        } else {
            word = hotwordService.queryHotWord().get(0);
            log.info("热点词 获取关联关系 默认参数={}", word);
        }
        List<HotwordInfo> result = hotwordService.queryHotWordAssociation(word);
        log.info("热点词 获取热门词关联词 结果={}", JSON.toJSONString(result));
        HashMap<String, Object> resultMap = new LinkedHashMap<>();
        resultMap.put("param", word);
        resultMap.put("association", result);
        return new AppReply(AppReply.SUCCESS_CODE, "查询成功", resultMap);
    }

    /**
     * getDataAssetListInfos
     * 获取所有的数据资产的标题 ，用于专题的专题
     *
     * @param
     * @return com.bonc.ioc.common.util.AppReply
     * @author wangqinglin
     * @date 2021/4/14 14:27
     * @change 2021/4/14 14:27 by q8541q@163.com for init
     * @since 1.0.0
     */
    @GetMapping("/getDataAssetListInfos")
    public AppReply getDataAssetListInfos() {
        return AppReply.success(dataAssetService.getDataAssetListInfos());
    }
}
